package com.jeehentor.generate.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeehentor.common.vo.OptionsVO;
import com.jeehentor.common.vo.Result;
import com.jeehentor.generate.dto.GenCommonDTO;
import com.jeehentor.generate.dto.PublishDTO;
import com.jeehentor.generate.dto.SysGenPageDTO;
import com.jeehentor.generate.entity.SysGenForm;
import com.jeehentor.generate.entity.SysGenPage;
import com.jeehentor.generate.entity.SysGenPageHistory;
import com.jeehentor.generate.service.ISysGenFormService;
import com.jeehentor.generate.service.ISysGenPageHistoryService;
import com.jeehentor.generate.service.ISysGenPageService;
import com.jeehentor.generate.vo.SysGenPageDetailVO;
import com.jeehentor.utils.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Tag(name = "页面配置主表")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysGenPage")
public class SysGenPageController {

    private final ISysGenPageService sysGenPageService;
    private final ISysGenFormService sysGenFormService;
    private final ISysGenPageHistoryService sysGenPageHistoryService;

    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result<String> save(@RequestBody SysGenPage sysGenPage){
        if(StringUtils.isBlank(sysGenPage.getFormId())){
            return Result.error("表单id不能为空");
        }
        SysGenForm byId = sysGenFormService.getById(sysGenPage.getFormId());
        if(byId == null){
            return Result.error("表单id不存在");
        }
        LambdaQueryWrapper<SysGenPage> sysGenPageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysGenPageLambdaQueryWrapper.eq(SysGenPage::getPageName,sysGenPage.getPageName());
        SysGenPage one = sysGenPageService.getOne(sysGenPageLambdaQueryWrapper);
        if(one != null){
            return Result.error("页面名称已存在");
        }
        sysGenPage.setModelId(byId.getModelId());
        sysGenPage.setCreateBy(SecurityUtil.getCurrentUser().getId());
        sysGenPageService.save(sysGenPage);
        return Result.ok("保存成功");
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody SysGenPage sysGenPage){
        sysGenPageService.removeById(sysGenPage);
        return Result.ok("删除成功");
    }


    @Operation(summary = "修改")
    @PostMapping("/update")
    public Result<String> update(@RequestBody SysGenPage sysGenPage){
        LambdaQueryWrapper<SysGenPage> sysGenPageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysGenPageLambdaQueryWrapper.eq(SysGenPage::getPageName,sysGenPage.getPageName());
        sysGenPageLambdaQueryWrapper.ne(SysGenPage::getId,sysGenPage.getId());
        SysGenPage one = sysGenPageService.getOne(sysGenPageLambdaQueryWrapper);
        if(one != null){
            return Result.error("页面名称已存在");
        }
        SysGenPage byId = sysGenPageService.getById(sysGenPage.getId());
        if(ObjectUtil.isNull(byId)){
            return Result.error("页面id不存在");
        }
        SysGenPageHistory sysGenPageHistory = new SysGenPageHistory();
        BeanUtils.copyProperties(sysGenPage,sysGenPageHistory);
        sysGenPageHistory.setId(null);
        sysGenPageHistory.setPageId(sysGenPage.getId());
        sysGenPageHistoryService.save(sysGenPageHistory);
        BigDecimal number = new BigDecimal(byId.getVersion());
        BigDecimal add = number.add(new BigDecimal("0.1"));
        String newVer = String.valueOf(add);
        sysGenPage.setVersion(newVer);
        sysGenPage.setUpdateBy(SecurityUtil.getCurrentUser().getId());
        sysGenPageService.updateById(sysGenPage);
        return Result.ok("修改成功");
    }

    @Operation(summary = "分页查询")
    @PostMapping("/selectPage")
    public Result<IPage<SysGenPage>> selectPage(@RequestBody SysGenPageDTO sysGenPageDTO){
        LambdaQueryWrapper<SysGenPage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(sysGenPageDTO.getPageName()),SysGenPage::getPageName,sysGenPageDTO.getPageName());
        return Result.OK(sysGenPageService.page(sysGenPageDTO.getPage(),queryWrapper));
    }


    @Operation(summary = "查询详情")
    @PostMapping("/selectPageById")
    public Result<SysGenPageDetailVO> selectPageDetail(@RequestBody SysGenPage sysGenPage){
        SysGenPageDetailVO sysGenPageDetailVO = sysGenPageService.selectPageDetail(sysGenPage.getId());
        return Result.OK(sysGenPageDetailVO);
    }

    @PostMapping("/getPageOptions")
    @Operation(summary = "模型下拉查询")
    public Result<List<OptionsVO>> getModelOptions(@RequestBody SysGenPageDTO sysGenPageDTO) {
        LambdaQueryWrapper<SysGenPage> sysGenModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysGenModelLambdaQueryWrapper.like(StringUtils.isNotBlank(sysGenPageDTO.getPageName()), SysGenPage::getPageName, sysGenPageDTO.getPageName());
        List<SysGenPage> list = sysGenPageService.list(sysGenModelLambdaQueryWrapper);
        ArrayList<OptionsVO> optionsVOS = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            list.forEach(x->{
                OptionsVO optionsVO = new OptionsVO();
                optionsVO.setLabel(x.getPageName());
                optionsVO.setValue(x.getId());
                optionsVOS.add(optionsVO);
            });
        }
        return Result.OK(optionsVOS);
    }

    @PostMapping("/selectByModel")
    @Operation(summary = "动态查询")
    public Result<Page<Map<String,Object>>> selectByModel(@RequestBody GenCommonDTO dto) {
        Page<Map<String,Object>> res = sysGenPageService.selectByModel(dto);
        return Result.ok(res);
    }

    @PostMapping("/saveByModel")
    @Operation(summary = "动态保存")
    public Result<String> saveByModel(@RequestBody GenCommonDTO dto) {
        sysGenPageService.saveByModel(dto);
        return Result.ok("保存成功");
    }

    @PostMapping("/updateByModel")
    @Operation(summary = "动态更新")
    public Result<String> updateByModel(@RequestBody GenCommonDTO dto) {
        sysGenPageService.updateByModel(dto);
        return Result.ok("修改成功");
    }

    @PostMapping("/deleteByModel")
    @Operation(summary = "动态删除")
    public Result<String> deleteByModel(@RequestBody GenCommonDTO dto) {
        sysGenPageService.deleteByModel(dto);
        return Result.ok("删除成功");
    }

    @PostMapping("/publish")
    @Operation(summary = "发布")
    public Result<String> publish(@RequestBody PublishDTO dto) {
        sysGenPageService.publish(dto);
        return Result.ok("发布成功");
    }
}
